package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

@WebServlet("/api/company-job-list.json")
public class CompanyDetail extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String cid = req.getParameter("cid");
        System.out.println(cid);
        String ruid = null;
        List<CompanyLs> companylist = new ArrayList<>();
        CompanyLs companyLs = new CompanyLs();
        Object result;
        result = new Object(){
            public boolean success = true;
            public Object data = companylist;
        };
        try(Connection c = DBUtil.getConnection()) {
            {
                String sql = "SELECT * FROM company WHERE cid = ? ORDER BY ruid  DESC";
                try (PreparedStatement s = c.prepareStatement(sql)) {
                    s.setString(1, cid);
                    try (ResultSet rs = s.executeQuery()) {
                        while (rs.next()) {

                            companyLs.ruid = rs.getString("ruid");
                            companyLs.gtitle = rs.getString("gtitle");
                            companyLs.gsalary=rs.getString("gsalary");
                            companyLs.gcontents = rs.getString("gcontents");
                            ruid = rs.getString("ruid");
                           companylist.add(companyLs);
                        }
                    }
                }
            }{
                String sql = "SELECT * FROM ruser WHERE ruid = ? ORDER BY ruid  DESC";
                try (PreparedStatement s = c.prepareStatement(sql)) {
                    s.setString(1, ruid);
                    try (ResultSet rs = s.executeQuery()) {
                        while (rs.next()) {

                            companyLs.rname = rs.getString("rname");
                            companyLs.rphone = rs.getString("rphone");
                           companyLs.remail = rs.getString("remail");
                            companylist.add(companyLs);
                        }
                    }
                }
            }

        } catch (SQLException exception) {
            exception.printStackTrace();
        }
        String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);
    }
}

